package com.sgzn.sgpay.service.tcp.receive.handler;

import com.sgzn.sgpay.service.tcp.receive.TcpReceiveCallHandlerTemplate;
import com.sgzn.sgpay.service.tcp.receive.modle.TcpReceiveCallRequest;
import com.sgzn.sgpay.service.tcp.receive.modle.response.TcpClockingQueryTotalBillResult;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 定时查询总账应答 处理器
 * @author huangsenguang
 * @version Id: TcpClockingQueryTotalBillHandler.java, v 0.1 2018/7/30 15:06 huangsenguang Exp $$
 */
@Service
@Slf4j
public class TcpClockingQueryTotalBillHandler extends TcpReceiveCallHandlerTemplate<TcpClockingQueryTotalBillResult> {

    /**
     * 协议总字符数
     */
    @Getter
    private final int protocolLength = 46;

    @Override
    public TcpClockingQueryTotalBillResult callInner(TcpReceiveCallRequest request) {
        String data = request.getData();
        TcpClockingQueryTotalBillResult result = new TcpClockingQueryTotalBillResult();
        result.setDeviceState(data.substring(0,2));
        result.setBabyLocalIncomeTotalBill(parse(data,2,10));
        result.setRefundTotal(parse(data,10,18));
        result.setExchangeLocalIncomeTotalBill(parse(data,18,26));
        result.setOnlineCoinAmount(parse(data,26,34));
        return result;
    }

    private int parse(String data, int beginIndex, int endIndex) {
        return Integer.parseInt(SgTcpUtil.sortTcp(data.substring(beginIndex, endIndex)), 16);
    }

    @Override
    protected int getProtocolLength(TcpReceiveCallRequest request) {
        return this.protocolLength;
    }

}

